﻿from PySide.QtCore import QObject
from karmadbg.uicore.uicmd import registerUiCommand
from karmadbg.dbgcore.util import showtraceback

import sys

class WidgetManager(QObject) :

    def __init__(self, dbgsettings, uimanager):
        
        super(WidgetManager,self).__init__()

        self.dbgSettings = dbgsettings
        self.uimanager = uimanager
        self.widgets = {}

        for widgetSetting in dbgsettings.widgets:
            self.widgets[ widgetSetting.name ] = self.constructWidget(widgetSetting)

        registerUiCommand("listWidgets", "list widgets", self.widgetListCommand)
        registerUiCommand("unregisterWidget", "unregister widget", self.widgetUnregisterCommand)
        registerUiCommand("registerWidget", "register widget", self.widgetRegisterCommand)
        

    def constructWidget(self, widgetSettings):
        module = __import__( widgetSettings.module, fromlist=[widgetSettings.className])
        classobj = getattr(module, widgetSettings.className)
        obj = classobj(widgetSettings, self.uimanager)
        obj.behaviour = widgetSettings.behaviour
        obj.setObjectName(widgetSettings.name)
        obj.widgetSettings = widgetSettings

        if not widgetSettings.invisible:
            obj.setVisible(widgetSettings.visible)

        if hasattr(obj, "constructDone"): obj.constructDone()
        if widgetSettings.title:
            obj.setWindowTitle(widgetSettings.title)

        return obj

    def __getitem__(self,name):
        return self.widgets[name]

    def __contains__(self,name):
        return name in self.widgets

    def values(self):
        return self.widgets.values()
        
    def unregisterWidget(self, name):

        if name in self.widgets:
            widgetObj = self.widgets[name]
            widgetObj.close()
            widgetObj.unregister(self.uimanager, widgetObj.widgetSettings)
            self.dbgSettings.removeWidget(widgetObj.widgetSettings)
            del self.widgets[name]

        self.uimanager.saveSettings()
        
    def registerWidget(self, moduleName, className, widgetName):
        module = __import__( moduleName, fromlist=[className])
        classobj = getattr(module, className)
        widgetSettings = self.dbgSettings.addWidget(moduleName, className, widgetName)
        classobj.register(self.uimanager, widgetSettings)
        self.widgets[widgetName] = self.constructWidget(widgetSettings)

        self.uimanager.saveSettings()
                 
    def widgetListCommand(self):
        for widgetName in self.widgets.keys():
            print widgetName
            
    def widgetUnregisterCommand(self, *args):
        try:  
            if len(args)==0:
                print "wiget name not noted"
            elif not args[0] in self.widgets:
                print "widget not found"
            else:
                self.unregisterWidget(args[0])
            
        except:       
            print "faild to unregister widget"
            
    def widgetRegisterCommand(self, *args):
        try:
            if len(args) != 3:
                 print "You should note module name, class name and widget name"
            else:
                self.registerWidget(*args)
        except:
            print showtraceback(sys.exc_info() )
            print "failed to register widget"
